package com.snail.consumer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.snail.constants.RabbitMqConstants;
import com.snail.mapper.BizContentReviewMapper;
import com.snail.mapper.BizFilesMapper;
import com.snail.mapper.BizWorksMapper;
import com.snail.model.dto.media.FileMergeDto;
import com.snail.model.pojo.media.BizContentReview;
import com.snail.model.pojo.media.BizFiles;
import com.snail.model.pojo.media.BizWorks;
import com.snail.service.BizFilesService;
import com.snail.utils.AliYunGreenUtils;
import com.snail.utils.GreenResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Monica
 * @description
 * @date 2025/8/13 19:45
 */
@Component
@Slf4j
public class MediaConsumer {

    @Autowired
    private BizContentReviewMapper bizContentReviewMapper;
    @Autowired
    private BizWorksMapper bizWorksMapper;
    @Value("${minio.endpoint}")
    private String endpoint;
    @Autowired
    private BizFilesMapper bizFilesMapper;

    @Autowired
    private AliYunGreenUtils aliyunGreenUtils;

    @Transactional
    @RabbitListener(queues = RabbitMqConstants.MINIO_REVIEW_QUEUE)
    public void mergeChunks(BizWorks message) throws IOException {
        if (message.getStatus() != 2) {
            return;
        }
        LambdaQueryWrapper<BizFiles> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizFiles::getFileMd5, message.getFileMd5());
        BizFiles bizFiles = bizFilesMapper.selectOne(queryWrapper);


        GreenResponse greenResponse = aliyunGreenUtils.reviewVideo(endpoint + bizFiles.getPreviewUrl());
        BizContentReview contentReview = createContentReview(message, greenResponse);
        if (greenResponse.getCode() == 200) {
            message.setStatus(3);
            bizWorksMapper.updateById(message);

        }
    }

    private BizContentReview createContentReview(BizWorks bizWorks, GreenResponse greenResponse) {
        LambdaQueryWrapper<BizFiles> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizFiles::getFileMd5, bizWorks.getFileMd5());
        BizFiles bizFiles = bizFilesMapper.selectOne(queryWrapper);
        BizContentReview bizContentReview = new BizContentReview();
        bizContentReview.setContentId(bizWorks.getId());
        bizContentReview.setContentUrl(endpoint + bizFiles.getPreviewUrl());
        bizContentReview.setContentHash(bizFiles.getFileMd5());
        bizContentReview.setSubmitterId(bizWorks.getUserId());
        bizContentReview.setSubmitTime(LocalDateTime.now());
        bizContentReview.setReviewType(0);
        if (greenResponse.getCode() == 200) {
            bizContentReview.setReviewStatus(0);
        } else {
            bizContentReview.setReviewStatus(1);
        }
        bizContentReview.setReviewTime(LocalDateTime.now());
        bizContentReview.setSystemReviewMsg(greenResponse.getMessage());
        bizContentReview.setTaskId(greenResponse.getTaskId());
        bizContentReviewMapper.insert(bizContentReview);
        return bizContentReview;
    }
}
